package com.wshl.aijiuunion.controller.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.blankj.utilcode.util.ActivityUtils;
import com.blankj.utilcode.util.BarUtils;
import com.blankj.utilcode.util.GsonUtils;
import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.SPUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.example.library.AutoFlowLayout;
import com.example.library.FlowAdapter;
import com.wshl.aijiuunion.GlobConstant;
import com.wshl.aijiuunion.NetUrl;
import com.wshl.aijiuunion.R;
import com.wshl.aijiuunion.callback.RequestCallback;
import com.wshl.aijiuunion.model.RequestModel;
import com.wshl.aijiuunion.view.adapter.HIstoryAdapter;
import com.wshl.aijiuunion.view.bean.SearchBean;
import com.wshl.aijiuunion.view.widget.NofastClickTextview;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.bingoogolapple.baseadapter.BGAOnRVItemClickListener;

/**
 * Created buy 风凊扬 on 2019/11/14 0014
 */
public class SearchActivity extends AppCompatActivity {
    @BindView(R.id.img_fanhui)
    ImageView imgFanhui;
    @BindView(R.id.et_searchcontent)
    EditText etSearchcontent;
    @BindView(R.id.tv_search)
    NofastClickTextview tvSearch;
    @BindView(R.id.ll_top)
    LinearLayout llTop;
    @BindView(R.id.recycle_search)
    RecyclerView recycleSearch;
    @BindView(R.id.auto_flowlayout)
    AutoFlowLayout autoFlowlayout;

    private RequestModel requestModel;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN | WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE | WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);

        setContentView(R.layout.activity_search);
        ButterKnife.bind(this);
        com.wshl.aijiuunion.utils.BarUtils.setStatusBarForActivity(this);
        recycleSearch.setLayoutManager(new LinearLayoutManager(this));
        requestModel = new RequestModel();


    }

    @Override
    protected void onResume() {
        super.onResume();

        getSearchData();
    }

    private void getSearchData() {
        requestModel.search(this, new RequestCallback() {
            @Override
            public void onSuccess(String response) {

                SearchBean searchBean = GsonUtils.fromJson(response, SearchBean.class);
                SearchBean.DataBean data = searchBean.getData();
                final List<SearchBean.DataBean.ListBean> data1 = data.getList();

                List<SearchBean.DataBean.HistoryBean> history = data.getHistory();
                if (history != null) {

                    final List<String> historydata = new ArrayList<>();
                    for (SearchBean.DataBean.HistoryBean historyBean : history) {

                        historydata.add(historyBean.getKeyword());
                    }
                    HIstoryAdapter hIstoryAdapter = new HIstoryAdapter(recycleSearch);
                    hIstoryAdapter.setOnRVItemClickListener(new BGAOnRVItemClickListener() {
                        @Override
                        public void onRVItemClick(ViewGroup parent, View itemView, int position) {
                            String s = historydata.get(position);
                            String token = SPUtils.getInstance().getString(GlobConstant.TOKEN);
                            startWeb(token, NetUrl.GOODSLIST, s);

                        }
                    });
                    hIstoryAdapter.setData(historydata);
                    recycleSearch.setAdapter(hIstoryAdapter);
                }

                if (data1 == null || data1.size() == 0) {

                    return;
                }
                autoFlowlayout.clearViews();
                autoFlowlayout.setAdapter(new FlowAdapter(data1) {
                    @Override
                    public View getView(int position) {
                        final SearchBean.DataBean.ListBean listBean = data1.get(position);
                        View item = View.inflate(SearchActivity.this, R.layout.item_autolayout, null);
                        TextView tvAttrTag = (TextView) item.findViewById(R.id.tv_item_topic);

                        tvAttrTag.setTextColor(getResources().getColor(R.color.gray_7));

                        tvAttrTag.setText(listBean.getKeyword());

                        tvAttrTag.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                //点击更多

                                String token = SPUtils.getInstance().getString(GlobConstant.TOKEN);
                                startWeb(token, NetUrl.GOODSLIST, listBean.getKeyword());

                            }
                        });

                        return item;
                    }
                });


            }

            @Override
            public void onError(int code) {
                LogUtils.e("搜索页面:" + code);
            }

            @Override
            public void onIntentError() {

            }
        });
    }

    private void startWeb(String token, String url, String keyword) {
        if (TextUtils.isEmpty(token)) {
            ToastUtils.showShort("请登录");
            return;
        }
        Intent intent = new Intent(SearchActivity.this, WebActivity.class);
        intent.putExtra(GlobConstant.WEBURL, url + token + "&keyword=" + keyword);
        ActivityUtils.startActivity(intent);
    }

    @OnClick({R.id.img_fanhui, R.id.tv_search, R.id.img_clear_history})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.img_clear_history:
                requestModel.clearHistory(SearchActivity.this, new RequestCallback() {
                    @Override
                    public void onSuccess(String response) {
                        getSearchData();
                    }

                    @Override
                    public void onError(int code) {
                        ToastUtils.showLong("删除失败:" + code);
                    }

                    @Override
                    public void onIntentError() {

                    }
                });
                break;
            case R.id.img_fanhui:
                this.finish();
                break;
            case R.id.tv_search:
                String input = etSearchcontent.getText().toString().trim();
                if (TextUtils.isEmpty(input)) {
                    ToastUtils.showLong("请输入搜索内容");
                    return;
                }

                String token = SPUtils.getInstance().getString(GlobConstant.TOKEN);
                startWeb(token, NetUrl.GOODSLIST, input);

                break;
        }
    }


}
